import request from "@/utils/request.js";
import { ElMessage } from "element-plus";

//获取入伍总人数
export function getArmyCount() {
  return request({
    url: "/admin/joinArmy/totalCount",
    method: "GET",
  });
}

//分页条件查询
export function getPage(data) {
  return request({
    url: "/admin/joinArmy/members",
    method: "POST",
    data,
  });
}

//导出学生列表信息
export function exportList(data) {
  return request({
    url: "/admin/preReserve/membersExcelList",
    method: "POST",
    responseType: "blob",
    data,
  })
    .then((response) => {
      const url = window.URL.createObjectURL(new Blob([response.data]));
      const link = document.createElement("a");
      link.href = url;
      link.setAttribute("download", "selected_members_list.xlsx"); // 设置下载文件名
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
      ElMessage.success("导出成功！");
    })
    .catch((error) => {
      console.error("导出失败", error);
      ElMessage.error("导出失败！");
    });
}

//导出勾选学生Excel简历
export function exportExcel(data) {
  return request({
    url: "/admin/preReserve/membersExcel",
    method: "POST",
    responseType: "blob",
    data,
  })
    .then((response) => {
      //从响应头获取文件名
      const contentDisposition = response.headers["content-disposition"];
      let fileName = "preReserve_members_PDF.zip"; // 默认文件名
      if (contentDisposition && contentDisposition.includes("filename=")) {
        fileName = contentDisposition.split("filename=")[1];
      }
      const url = window.URL.createObjectURL(new Blob([response.data]));
      const link = document.createElement("a");
      link.href = url;
      link.setAttribute("download", fileName); // 使用后端返回的文件名
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
      ElMessage.success("导出成功！");
    })
    .catch((error) => {
      ElMessage.error("导出失败！");
      console.error("导出失败", error);
    });
}

//导出勾选学生的pdf
export function exportPdf(data) {
  return request({
    url: "/admin/preReserve/membersPDF",
    method: "POST",
    data,
    responseType: "blob", // 确保响应类型为 blob
  })
    .then((response) => {
      const contentDisposition = response.headers["content-disposition"];
      let fileName = ""; // 默认文件名
      if (contentDisposition && contentDisposition.includes("filename=")) {
        fileName = contentDisposition.split("filename=")[1];
      }
      const url = window.URL.createObjectURL(new Blob([response.data]));
      const link = document.createElement("a");
      link.href = url;
      link.setAttribute("download", fileName); // 使用后端返回的文件名
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
      ElMessage.success("导出成功！");
    })
    .catch((error) => {
      ElMessage.error("导出失败！");
      console.error("导出失败", error);
    });
}

//从预储或者预征获取成员信息
export function getMemberInfo(data) {
  return request({
    url: "/admin/joinArmy/joinArmyInfo",
    method: "POST",
    data,
  });
}

//条件分页查询预储或者预征班
export function getPagePOrP(url, params) {
  return request({
    url,
    method: "GET",
    params,
  });
}

//上传图片
export function upLodaImg(url, formData, config) {
  return request.post(url, formData, config);
}

//提交新入伍成员信息
export function submitMemberInfo(form) {
  return request({
    url: "/admin/joinArmy/newMember",
    method: "POST",
    data: form,
  });
}

//将预储或预征班成员转为入伍成员
export function transformMember(data) {
  return request({
    url: "/admin/joinArmy/addJoinArmy",
    method: "POST",
    data,
  });
}

// 查询个人简历信息
export function getJoinArmyInfo(id) {
  return request({
    url: "/admin/joinArmy/personDetailInfo",
    method: "GET",
    params: {
      id,
    },
  });
}
